package views;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.Time;
import java.text.SimpleDateFormat;

import javax.swing.*;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import controllers.TimeSlot;

import db.TimeSlotDAO;

public class ViewTimeSlotsFrame extends JFrame {

	private static final int WIDTH=800;
	private static final int HEIGHT=500;
	private static final String TITLE = "View / Edit Time Slots";
	private static Object[][] timeSlots;
	private static JButton delete, add, edit, save, clear;
	private static JTable table;
	private static java.util.List<TimeSlot> list;
	private static MyTableModel model;
	private static JScrollPane scrollpane;
	private static Container c;
	private static int editRow;
	private static SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");

	public ViewTimeSlotsFrame(int xBound, int yBound){
		this.setBounds(xBound+100, yBound+50, WIDTH, HEIGHT);
		this.setResizable(false);
		this.setTitle(TITLE);
		

		c = this.getContentPane();
		c.setLayout(null);


		scrollpane = new JScrollPane();
		c.add(scrollpane);
		updateRows();

		add = new JButton("Add");
		add.setBounds(25, 425, 100, 30);
		add.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				new AddTimeSlotFrame(getX(),getY());
			}
		});
		c.add(add);

		edit = new JButton("Edit");
		edit.setBounds(135, 425, 100, 30);
		edit.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				int row = table.getSelectedRow();
				if(row!=-1){
					model.editRow(row);
					editRow = row;
					table.editCellAt(row, table.getSelectedColumn());
					table.getCellEditor().addCellEditorListener(new CellEditorListener(){
						@Override
						public void editingCanceled(ChangeEvent e) {
						}
						@Override
						public void editingStopped(ChangeEvent e) {
							save.setEnabled(true);
							edit.setEnabled(false);
						}
					});
				}
				else{
					System.out.println("Nothing Selected");
				}

			}
		});
		c.add(edit);

		delete = new JButton("Delete");
		delete.setBounds(245, 425, 100, 30);
		delete.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(JOptionPane.showConfirmDialog(getComponent(0), "Are you sure you want to delete? This cannot be undone", "Delete", 0)==0){
					int row = table.getSelectedRow();
					if(row!=-1){
						TimeSlotDAO.deleteTimeSlot(list.get(row));
						updateRows();
					}
					else{
						System.out.println("Nothing selected");
					}
				}
			}
		});

		c.add(delete);

		save = new JButton("Save");
		save.setBounds(355, 425, 100, 30);
		save.setEnabled(false);
		save.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try{
					TimeSlot temp = list.get(editRow);
					temp.setWeekday(table.getValueAt(editRow, 0).toString());
					Time t2;
					t2 = new Time(new SimpleDateFormat("hh:mm:ss").parse(table.getValueAt(editRow, 1).toString()).getTime());
					temp.setStartTime(t2);
					Time t;
					t = (Time)table.getValueAt(editRow, 2);
					temp.setEndTime(t);
					TimeSlotDAO.saveTimeSlot(temp);
					save.setEnabled(false);
					edit.setEnabled(true);
				}
				catch(Exception e){
					e.printStackTrace();
					JOptionPane.showMessageDialog(getComponent(0), "Save Failed! Please make sure your changes are valid.","Save Failed!",0);
					updateRows();
					edit.setEnabled(true);
					save.setEnabled(false);
				}
			}
		});
		c.add(save);
		
		clear = new JButton("Clear");
		clear.setBounds(465, 425, 100, 30);
		clear.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				updateRows();
				edit.setEnabled(true);
				save.setEnabled(false);
			}
		});
		c.add(clear);

		this.setVisible(true);

	}

	public static void updateRows(){
		list = TimeSlotDAO.getTimeSlots();
		timeSlots = new Object[list.size()][4];
		for(int i=0;i<list.size();i++){
			timeSlots[i][0] = list.get(i).getWeekday();
			timeSlots[i][1] = sdf.format(list.get(i).getStartTime());
			timeSlots[i][2] = sdf.format(list.get(i).getEndTime());
			timeSlots[i][3] = list.get(i).getDriverName();
		}

		Object columnNames[] = {"WeekDay", "Start Time", "End Time", "Driver"};
		model = new MyTableModel(timeSlots, columnNames);
		table = new JTable(model);
		table.setRowHeight(30);
		table.getTableHeader().setReorderingAllowed(false);
		//table.addRowSelectionInterval(0, 0);
		TableColumn col = table.getColumnModel().getColumn(0);
		String[] weekDays = {"Monday",
				"Tuesday",
				"Wednesday",
				"Thursday",
				"Friday",
				"Saturday",
				"Sunday"}; 
		col.setCellEditor(new customWeekDayEditor(new JComboBox(weekDays)));
		c.remove(scrollpane);
		scrollpane = new JScrollPane(table);
		scrollpane.setBounds(25,0,750,400);
		c.add(scrollpane);
	}


}

class customWeekDayEditor extends DefaultCellEditor{
	public customWeekDayEditor(JComboBox arg0) {
		super(arg0);
	}
	
}
